package mirrormonkey.rpc;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import mirrormonkey.rpc.proxy.EntityProxy;
import mirrormonkey.rpc.tools.multispec.entities.ImplementingSpecs;
import mirrormonkey.rpc.tools.multispec.specs.FirstSpec;
import mirrormonkey.rpc.tools.multispec.specs.SecondSpec;

import org.junit.Test;

/**
 * Contains test cases concerning entities that implement multiple
 * <tt>RpcSpecifications</tt>.
 * 
 * @author Philipp Christian Loewner
 * 
 */
public class MultiSpecTest extends SingleClientTestProto {

	@Test
	public void test() {
		sleepNoInterrupt(1000);
		ImplementingSpecs serverEntity = new ImplementingSpecs();
		EntityProxy<FirstSpec> firstProxy = serverApplication.rpcModule
				.createRpcProxy(FirstSpec.class);
		firstProxy.addTargetEntity(serverEntity);
		firstProxy.addMessageConnection(getFirstConnection());
		EntityProxy<SecondSpec> secondProxy = serverApplication.rpcModule
				.createRpcProxy(SecondSpec.class);
		secondProxy.addTargetEntity(serverEntity);
		secondProxy.addMessageConnection(getFirstConnection());

		serverApplication.coreModule.addMapping(getFirstConnection(),
				serverEntity, ImplementingSpecs.class);

		sleepNoInterrupt(100);
		processEvents();

		ImplementingSpecs clientEntity = (ImplementingSpecs) clientApplication.appState.entityProvider
				.getData(serverEntity.getData().id).entity;

		sleepNoInterrupt(100);
		processEvents();

		assertFalse(clientEntity.firstFlag);
		assertFalse(clientEntity.secondFlag);
		assertFalse(clientEntity.sharedFlag);

		firstProxy.getCallTarget().firstMethod();
		sleepNoInterrupt(100);
		processEvents();
		assertTrue(clientEntity.firstFlag);
		assertFalse(clientEntity.secondFlag);
		assertFalse(clientEntity.sharedFlag);
		clientEntity.clear();

		firstProxy.getCallTarget().sharedMethod();
		sleepNoInterrupt(100);
		processEvents();
		assertFalse(clientEntity.firstFlag);
		assertFalse(clientEntity.secondFlag);
		assertTrue(clientEntity.sharedFlag);
		clientEntity.clear();

		secondProxy.getCallTarget().secondMethod();
		sleepNoInterrupt(100);
		processEvents();
		assertFalse(clientEntity.firstFlag);
		assertTrue(clientEntity.secondFlag);
		assertFalse(clientEntity.sharedFlag);
		clientEntity.clear();

		secondProxy.getCallTarget().sharedMethod();
		sleepNoInterrupt(100);
		processEvents();
		assertFalse(clientEntity.firstFlag);
		assertFalse(clientEntity.secondFlag);
		assertTrue(clientEntity.sharedFlag);
		clientEntity.clear();

		sleepNoInterrupt(1000);
	}

}
